//
//  NBUserAccountViewModel.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/22.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit

class NBUserAccountViewModel: NSObject {
    
    static let sharedViewModel: NBUserAccountViewModel = NBUserAccountViewModel()

    var userAccount :NBUserAccount?
    
    
    private override init() {
        super.init()
        userAccount = loadUserAccount()
    }
    
    var isUserLogin :Bool {
        if userAccount?.expires_date?.compare(NSDate()) == .OrderedDescending {
            return true
        }
        
        userAccount = nil
        return false
    
    }
    
    var headUrl:NSURL? {
        get{
            print(userAccount?.avatar_large!)
            return NSURL(string: userAccount?.avatar_large ?? "")
        }
    }
    
    
}

extension NBUserAccountViewModel{
    func loadAccessToken(code:String,finish:(isSuccess: Bool)->()) {
        ACNetworkTool.sharedTool.loadAccessToken(code) { (responseObject, error) in
            if error != nil{
                finish(isSuccess: false)
                
                return
            }else{
                
                guard let result = responseObject as? [String:AnyObject] else{
                    finish(isSuccess: false)
                    
                    return
                }
//                let access_token = result["access_token"]!
//                let uid = result["uid"]!
//                
//                let param = ["access_token": access_token, "uid": uid]
//                let urlStr = "https://api.weibo.com/2/users/show.json"
                ACNetworkTool.sharedTool.loadUserInfo(result, finish: finish, finished: { (responseObject, error) in
                    guard var dict = responseObject as? [String:AnyObject] else{
                        
                        finish(isSuccess: false)
                        return
                    }
                    for (key,value) in result{
                        dict[key] = value
                    }
                    
                    self.saveUserAccount(dict)
                    finish(isSuccess: true)
                })
                
//                ACNetworkTool.sharedTool.request(.GET, URLString: urlStr, parameters: param, finish: { (responseObject, error) in
//                    
//                    
//                    
//                })
            }
        }
    }
    
//        let urlStr = "https://api.weibo.com/oauth2/access_token"
//        let dict = ["client_id": client_id, "client_secret": client_secret, "grant_type": "authorization_code", "code": code, "redirect_uri": redirect_uri]
        
        
//        ACNetworkTool.sharedTool.request(.POST, URLString: urlStr, parameters: dict, finish: { (responseObject, error) in
            //                print(responseObject,error)
        
//            }
//        })
        
//    }
    
    func saveUserAccount(dict:[String:AnyObject]) {
        let userDefault = NSUserDefaults.standardUserDefaults()
        let userAccount = NBUserAccount(dict: dict)
        self.userAccount = userAccount
        let keys = ["access_token", "expires_in", "expires_date", "expires_date", "uid", "name", "avatar_large"]
        
        let userDict = userAccount.dictionaryWithValuesForKeys(keys)
        NBUserAccountViewModel.sharedViewModel.headUrl
        userDefault.setObject(userDict, forKey: "userAccount")
        userDefault.synchronize()
        print(NSHomeDirectory())
        
    }
    
    func loadUserAccount() -> NBUserAccount? {
        let userDefault = NSUserDefaults.standardUserDefaults()
        let dict = userDefault.objectForKey("userAccount") as? [String:AnyObject]
        guard let userDict = dict else{
            return nil
        }
        let userAccount = NBUserAccount(dict: userDict)
        
        return userAccount
        
        
    }
}
